package com.ponhu.ea2.goodsCenter.provider.service;

import com.google.common.collect.Lists;
import com.ponhu.ea2.enums.SyncAppletTypeEnum;
import com.ponhu.ea2.goodsCenter.provider.utils.DingTalkHandler;
import com.ponhu.ea2.properties.GoodsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author luyu
 * @create 2022-03-21 16:31
 */
@Service
@Slf4j
public class PhinventoryToAmqpService {

    @Resource
    InventoryService inventoryService;

    @Resource
    RabbitTemplate amqpTemplate;

    @Resource
    GoodsProperties goodsProperties;

    @Resource
    DingTalkHandler dingTalkHandler;

    @Resource
    private GoodsDataCreateorupdateLogService goodsDataCreateorupdateLogService;

    public void pushDataToAmqp(List<String> inventoryCodes,Integer type){
        log.info("推送会员小程序开始,入参为：{},类型为：{}",inventoryCodes,type);
        if (BooleanUtils.isFalse(goodsProperties.getMqswitch())) return;

        try {
            CorrelationData correlationData = new CorrelationData();
            correlationData.setId(UUID.randomUUID().toString());
            if(type.equals(SyncAppletTypeEnum.GOODS_PRICE.getCode())){
                correlationData.setReturned(new ReturnedMessage(
                        new Message(null,new MessageProperties()),
                        type,inventoryCodes.toString(),
                        goodsProperties.getExchangesMap().get("appletExchanges"),
                        goodsProperties.getRoutingKeyMap().get("goodsDataKey")));
                Lists.partition(inventoryCodes, goodsProperties.getMqLimitMap().get("priceLimit"))
                        .stream().forEach(item -> {
                    amqpTemplate.convertAndSend(
                            goodsProperties.getExchangesMap().get("appletExchanges"),
                            goodsProperties.getRoutingKeyMap().get("priceKey"),
                            inventoryService.getGoodsPriceByCode(item),
                            correlationData);
                });
            }
            if(type.equals(SyncAppletTypeEnum.GOODS_STATUS.getCode())){// 推送上下架状态
                correlationData.setReturned(new ReturnedMessage(
                        new Message(inventoryCodes.toString().getBytes(),new MessageProperties()),
                        type,inventoryCodes.toString(),
                        goodsProperties.getExchangesMap().get("appletExchanges"),
                        goodsProperties.getRoutingKeyMap().get("goodsDataKey")));
                Lists.partition(inventoryCodes, goodsProperties.getMqLimitMap().get("statusLimit"))
                        .stream().forEach(item -> {
                    amqpTemplate.convertAndSend(
                            goodsProperties.getExchangesMap().get("appletExchanges"),
                            goodsProperties.getRoutingKeyMap().get("statusKey"),
                            inventoryService.getGoodsStatusByCode(item),
                            correlationData);
                });
            }
            if(type.equals(SyncAppletTypeEnum.PH_INVENTORY_DATA.getCode())){
                correlationData.setReturned(new ReturnedMessage(
                        new Message(inventoryCodes.toString().getBytes(),new MessageProperties()),
                        type,inventoryCodes.toString(),
                        goodsProperties.getExchangesMap().get("appletExchanges"),
                        goodsProperties.getRoutingKeyMap().get("goodsDataKey")));
                Lists.partition(inventoryCodes, goodsProperties.getMqLimitMap().get("goodsDataLimit"))
                        .stream().forEach(item -> {
                    amqpTemplate.convertAndSend(
                            goodsProperties.getExchangesMap().get("appletExchanges"),
                            goodsProperties.getRoutingKeyMap().get("goodsDataKey"),
                            inventoryService.getInventoryBasicByCode(item),
                            correlationData);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("推送会员小程序报错，货品编码为{},错误信息为{}",inventoryCodes,e);
            inventoryCodes.stream().forEach(item -> {
                goodsDataCreateorupdateLogService.saveLog("Applet","推送会员小程序失败",
                        item,String.valueOf(type));
                String msg = dingTalkHandler.getMsgForApplet(type, item,e.getMessage());
                dingTalkHandler.sendMsg(msg, new ArrayList());
            });
        }
        log.info("推送会员小程序结束,货品编码为{}",inventoryCodes);
    }
}
